const {Feedback, Op, Comment} = require("../db/seq");

class FeedBackServe {
    async getCommentListServe(ctx) {
        const {pageSize = 10, pageNum = 1, content} = ctx.request.query;
        const whereOptArr = []
        const whereOpt = {}
        content && Object.assign(whereOpt, {
            content: {
                // 模糊查询
                [Op.like]: '%' + content.trim() + '%'
            }
        });
        whereOptArr.push(whereOpt)
        const offset = (pageNum - 1) * pageSize;
        const {count, rows} = await Feedback.findAndCountAll({
            attributes: {exclude: ['updatedTime']},
            where: whereOptArr.length > 1 ? {
                [Op.and]: [
                    ...whereOptArr
                ]
            } : whereOpt,
            //分页
            offset,
            //限制
            limit: Number(pageSize),
            order: [
                ['createdTime', 'DESC']
            ]
        })
        return {
            total: count,
            data: rows,
            pageNum,
            pageSize
        }
    }

    async deleteFeedBackServe(id) {
        const res = await Feedback.destroy({
            where: {
                id
            }
        });
        return res > 0;
    }

    async getFeedBackByIdServe({id}) {
        const res = await Feedback.findOne({
            where: {
                id
            }
        });
        return res ? res.dataValues : null
    }

    async addFeedBackServe(params) {
        const res = await Feedback.create({
            ...params
        });
        return res.dataValues;
    }
}

module.exports = new FeedBackServe()
